package com.intelligentcar.springboot.controller;


import com.intelligentcar.springboot.bean.Linelist;
import com.intelligentcar.springboot.bean.Scheduling;
import com.intelligentcar.springboot.bean.Station;
import com.intelligentcar.springboot.bean.Vehicle;
import com.intelligentcar.springboot.service.StationServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.regex.Pattern;
import  java.util.regex.Matcher;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

//@Controller
// 在加一个 @RequestMapping  成为二级路径
@RestController
public class StationController {

    int delete;

    int chr;
    int chr2;

    @Autowired
    private StationServer stationServer;

//    @RequestMapping("/hello")
//    public String hello(){
//        System.out.println("myHellorrr");
//        return "hello";
//    }

//    @GetMapping("/hello2")
//    public String hello(@RequestParam("age")String age){
//        System.out.println("myHellorrr222");
//        System.out.println(age);
//        return age;
//    }

//    @RequestMapping("/driverWorkTime")
//    @ResponseBody
//    public  List<Scheduling> driverWorkTime(Scheduling scheduling){
//        List<Scheduling> schedulingList= userServer.findWorkTime();
//
//        if (schedulingList!=null){
//
//            return schedulingList;
//        }
//        return null;
//    }


    @RequestMapping("/map")
    @ResponseBody
    public int creatMap(@RequestBody Station station){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        station.setAddTime(df.format(new Date()));
        station.setStationState("1");
        if ( station.getSelectLngLat().equals("")){
            return 2;
        }
        if (station.getRid().equals("") || station.getStationName().equals("")){
            return 0;
        }
        return stationServer.insertStation(station);
    }

    @RequestMapping("/selectCountStation")
    @ResponseBody
    public int selectCountStation(@RequestBody Station station){
        System.out.println(station.getRid());
        int selectCountStation=stationServer.selectCountStation(station.getRid());
        System.out.println(selectCountStation);
        return selectCountStation+1;
    }





    @RequestMapping("/selectMap")
    @ResponseBody
    public  List<Station> driverWorkTime(@RequestBody Station station){
        if (String.valueOf(station.getId()).equals("") || station.getStationName().equals("")){
            List<Station> stationList =stationServer.selectStation();
            if (stationList!=null){
                return stationServer.selectStation();
            }
        }else if (!String.valueOf(station.getId()).equals("") || !station.getStationName().equals("")){
            List<Station> stationList =stationServer.selectStation2(station.getStationName(),station.getId());
            if (stationList!=null){
                return stationList;
            }
        }
        return null;
    }


    @RequestMapping("/selectLine")
    @ResponseBody
    public  List<Linelist> selectLine(@RequestBody Linelist linelist){
        if (!linelist.getLineId().equals("")){
            List<Linelist> linelistList=stationServer.selectOneLineList(linelist.getLineId());
            if (linelistList != null) {
                return stationServer.selectOneLineList(linelist.getLineId());
            }
        }
        else{
            System.out.println("123");
            List<Linelist> linelistList = stationServer.selectLine();
            if (linelistList != null) {
                return stationServer.selectLine();
            }
        }
        return null;
    }


    @RequestMapping("/insertLine")
    @ResponseBody
    public int insertLineLu(@RequestBody Linelist linelist){

        Pattern pattern =  Pattern.compile(".*\\d+.*");
        Matcher isNum = pattern.matcher(linelist.getInsertBeginLineName());
        Matcher isNum2 = pattern.matcher(linelist.getInsertEndLineName());
        if (isNum.matches() || isNum2.matches()){
            return -1;
        }


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        linelist.setAddTime(df.format(new Date()));   // || linelist.getLineId().equals("")
        if (linelist.getInsertBeginLineName().equals("") || linelist.getInsertEndLineName().equals("") ){
            return 0;
        }
        linelist.setInsertBeginLineName(linelist.getInsertBeginLineName()+"(公交站)");
        linelist.setInsertEndLineName(linelist.getInsertEndLineName()+"(公交站)");
        linelist.setCost(20);
        linelist.setSpendTime(30);
        linelist.setLineState("1");
        return stationServer.insertLine(linelist);
    }

    @RequestMapping("/deleteStation")
    @ResponseBody
    public  int deleteStation(@RequestBody Station station){
        System.out.println(station.getId());
        System.out.println(station.getStationName());
        if (!String.valueOf(station.getId()).equals("") || !station.getStationName().equals("")){
            int delete=stationServer.deleteStation(String.valueOf(station.getId()),station.getStationName());
            if (delete>=1){
                return 1;
            }
        }
        if (String.valueOf(station.getId()).equals("") && station.getStationName().equals("")){
            return 0;
        }
        return 0;
    }



    @RequestMapping("/updateStation")
    @ResponseBody
    public  int  updateStation(@RequestBody Station station) {
        if (!String.valueOf(station.getId()).equals("")) {
            if (!station.getStationName().equals("") && !station.getSelectLngLat().equals("")) {
                int update = stationServer.updateStation(station.getStationName(), station.getSelectLngLat(), station.getId());
                if (update == 1) {
                    return 1;
                }
            } else if (!station.getStationName().equals("") || !station.getSelectLngLat().equals("")) {
                int update = stationServer.updateStation(station.getStationName(), station.getSelectLngLat(), station.getId());
                if (update == 1) {
                    return 1;
                }
            }else{
                return 0;
            }
        } else{
            return 0;
        }
        return 0;
    }




    @RequestMapping("/deleteLine")
    @ResponseBody
    public  int  deleteLine(@RequestBody Linelist linelist ){    // Map<String,String[]> map
//        for (int i=0;i<map.get("lineState").length;i++) {
//            System.out.println(map.get("lineState")[i]);
//            System.out.println(map.get("luid")[i]);
//            delete=stationServer.deleteLine(map.get("lineState")[i],Long.valueOf(map.get("luid")[i]));
//        }
//        if (delete==1){
//            return 1;
//        }
//        return 0;
        if (!linelist.getLineId().equals("")){
            System.out.println(linelist.getLineId());
            int update=stationServer.deleteOneLineList(linelist.getLineId());
            if (update==1){
                return 1;
            }
        }
        return 0;
    }


    @RequestMapping("/selectLineId")
    @ResponseBody
    public  List<Linelist>  selectLineId(){
        List<Linelist> linelistList=stationServer.selectLineId();
        if (linelistList!=null){
            return linelistList;
        }
        return null;
    }


    @RequestMapping("/selectVehicleId")
    @ResponseBody
    public  List<Vehicle>  selectVehicleId(){
        List<Vehicle> vehicleList=stationServer.selectVehicleId();
        if (vehicleList!=null){
            return vehicleList;
        }
        return null;
    }

    @RequestMapping("/selectStationId")
    @ResponseBody
    public  List<Station>  selectStationId(){
        List<Station> stationList=stationServer.selectStationId();
        if (stationList!=null){
            return stationList;
        }
        return null;
    }


    @RequestMapping("/selectStationName")
    @ResponseBody
    public  List<Station>  selectStationName(){
        List<Station> stationList=stationServer.selectStationName();
        if (stationList!=null){
            return stationList;
        }
        return null;
    }


    @RequestMapping("/selectLineOne")
    @ResponseBody
    public  List<Linelist>  selectLineOne(){
        List<Linelist> linelistList=stationServer.selectLineList();
        if (linelistList!=null){
            return linelistList;
        }
        return null;
    }


    @RequestMapping("/updateLine")
    @ResponseBody
    public  int  selectLineOne(@RequestBody Linelist linelist){
        if (!linelist.getInsertBeginLineName().equals("")){
            linelist.setInsertBeginLineName(linelist.getInsertBeginLineName()+"(公交站)");
        }
        if (!linelist.getInsertEndLineName().equals("")){
            linelist.setInsertEndLineName(linelist.getInsertEndLineName()+"(公交站)");
        }
        if (!linelist.getInsertBeginLineName().equals("") || !linelist.getInsertEndLineName().equals("") ) {
            int updateLine = stationServer.updateLine(linelist.getInsertBeginLineName(), linelist.getInsertEndLineName(), linelist.getLineId());
            if (updateLine > 0) {
                return 1;
            }
        }
        return 0;
    }

    //查询站点表种的所有站点
    @RequestMapping ("/selectstation")
    @ResponseBody
    public Map selectstation (){
        Map map = new HashMap();
        map.put("list",stationServer.selectstation());
        return  map;
    }

}
